﻿using System;
using System.Linq;

namespace ECommerce.Controls
{
	public partial class TopCategoryList : BaseUserControl
	{
		private Service.ICategoryService _CategoryService;

		public string SelectedCategoryID
		{
			get
			{
				return this.ListTopCategories.SelectedValue;
			}
		}

		public string CssClass { get; set; }

		protected void Page_Load(object sender, EventArgs e)
		{
			this.ListTopCategories.CssClass = CssClass;
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindCategory();
		}

		private void BindCategory()
		{
			if (SiteContext.Current != null && SiteContext.Current.WorkingLanguage != null)
			{
				int currentLanguageID = SiteContext.Current.WorkingLanguage.LanguageId;

				if (_CategoryService == null)
				{
					_CategoryService = new Service.CategoryService();
				}

				var categoryByLang = _CategoryService.GetCategoryByLanguage(currentLanguageID);

				if (categoryByLang != null)
				{
					var categories = (from c in categoryByLang
									  where c.ParentCategoryID <= 0
									  select c);

					var topAllCategory = new Data.Entity.Category();
					topAllCategory.CategoryName = GetLocalResourceString("HeaderSearch.AllCategoryText");
					topAllCategory.CategoryID = 0;

					System.Collections.Generic.List<Data.Entity.Category> data = new System.Collections.Generic.List<Data.Entity.Category>();

					data.Add(topAllCategory);
					data.AddRange(categories);

					this.ListTopCategories.DataSource = data;
					this.ListTopCategories.DataBind();
				}
				else
				{
					this.ListTopCategories.DataSource = null;
					this.ListTopCategories.DataBind();
				}
			}
		}
	}
}